﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.MassLine
{
    public partial class MassHonourList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "MassHonourView";
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {      
            // 权限检查
            CheckPowerWithButton("MassHonourAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("MassHonourEdit.aspx?", "新增");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

        }
        private void LoadData()
        {
            var ds = PartyArticle.GetMassHonourArticle(Grid1.PageIndex, Grid1.PageSize, ddlStauts.SelectedValue, txtTitle.Text,BranchID);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("MassHonourEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("MassHonourDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string MassHonourId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("MassHonourDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyArticles.FirstOrDefault(t => t.ID == MassHonourId);
                DB.PartyArticles.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }

        public string GetFlags(string flag)
        {
            string returnValue = string.Empty;
            switch (flag)
            {
                case "0":
                    returnValue = "无";
                    break;
                case "1":
                    returnValue = "置顶";
                    break;
                case "2":
                    returnValue = "固顶";
                    break;
                case "3":
                    returnValue = "置顶+固顶";
                    break;
            }
            return returnValue;
        }
        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("MassHonourDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyArticles.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyArticles.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}